package scau.erps.view.system.team;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowFilter;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import scau.erps.app.Erps;
import scau.erps.entity.Bidding;
import scau.erps.entity.SystemOrder;
import scau.erps.service.BiddingService;
import scau.erps.service.SystemOrderService;
import scau.erps.service.impl.BiddingServiceImpl;
import scau.erps.service.impl.SystemOrderServiceImpl;
import scau.erps.util.CalculateQuarter;

/**
 * 我的竞价——小组
 * @author DCJ
 *
 */
public class MyOrderBiddingPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	private JPanel contentPanel,actionPanel;
	private JComboBox<String> yearCombo,productCombo,marketCombo;
	private JButton filterBtn,deleteBtn,refreshBtn;
	private JTable table;
	private JScrollPane dataPanel;
	private DefaultTableCellRenderer render;
	private BiddingListModel dataModel;
	public MyOrderBiddingPanel(){
		yearCombo = new JComboBox<String>(new String[]{"选择年份","Y1","Y2","Y3","Y4","Y5","Y6","Y7","Y8"});
		productCombo = new JComboBox<String>(new String[]{"选择产品","Beryl","Crystal","Ruby","Sapphire"});
		marketCombo = new JComboBox<String>(new String[]{"选择市场","本地","区域","国内","亚洲","国际"});		
		filterBtn = new JButton("筛选");		
		deleteBtn = new JButton("删除");
		refreshBtn = new JButton("刷新");			
		filterBtn.addActionListener(this);
		deleteBtn.addActionListener(this);
		refreshBtn.addActionListener(this);
		actionPanel = new JPanel(); //顶部交互操作面板
		actionPanel.add(yearCombo);
		actionPanel.add(productCombo);
		actionPanel.add(marketCombo);
		actionPanel.add(filterBtn);			
		actionPanel.add(refreshBtn);
		actionPanel.add(deleteBtn);
		table = new JTable();
		table.setPreferredScrollableViewportSize(new Dimension(900, 450));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		dataPanel = new JScrollPane(table);
		dataPanel.setViewportView(table);
		render = new DefaultTableCellRenderer();   
		render.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class,render);
		contentPanel = new JPanel();
  		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(actionPanel,BorderLayout.NORTH);
		contentPanel.add(dataPanel,BorderLayout.CENTER);
		this.add(contentPanel);
		this.setVisible(true);
		this.refresh();		
	}
	/**
	 * 刷新
	 */
	private void refresh(){
		this.dataModel = getBiddingListModel();
		this.table.setModel(dataModel);
		TableColumn column = table.getColumnModel().getColumn(0); //隐藏第一列
		column.setMinWidth(0);   
		column.setMaxWidth(0);
	}
	/**
	 * 加载数据模型
	 * @return
	 */
	private BiddingListModel getBiddingListModel(){
		BiddingService biddingService = Erps.getIocInstance().get(BiddingServiceImpl.class);
		String year = CalculateQuarter.getCurrentYear(Erps.getSand().getTime());
		List<Bidding> list = biddingService.getBiddings(Erps.getCurrentTeam().getId(),year,null);
		dataModel = new BiddingListModel(list);		
		return dataModel;
				
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==filterBtn){
			final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
			List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
			String year = null;
			if(yearCombo.getSelectedIndex() != 0 ){
				year = (String) yearCombo.getSelectedItem();
			}
			String product = null;
			if(productCombo.getSelectedIndex() != 0 ){
				product = (String) productCombo.getSelectedItem();
			}
			String market = null;
			if(marketCombo.getSelectedIndex() != 0 ){
				market = (String) marketCombo.getSelectedItem();
			}
			if(year !=null){
				filters.add(RowFilter.regexFilter(year));
			}
			if(product !=null){
				filters.add(RowFilter.regexFilter(product));
			}
			if(market !=null){
				filters.add(RowFilter.regexFilter(market));
			}
			sorter.setRowFilter(RowFilter.andFilter(filters));
			this.table.setRowSorter(sorter); 
            this.table.setModel(dataModel);
		}
		else if(e.getSource()==deleteBtn){
			int row = this.table.getSelectedRow();
			if (row == -1) {
				JOptionPane.showMessageDialog(this, "未选中任何行");
			} else {				
				int n = JOptionPane.showConfirmDialog(null, "删除竞价单不会退还广告费，确认删除选中竞价单？", "提醒",JOptionPane.OK_CANCEL_OPTION);
				if(n == JOptionPane.OK_OPTION){
					int id = (int) this.table.getValueAt(row, 0);
					BiddingService biddingService = Erps.getIocInstance().get(BiddingServiceImpl.class);
					Bidding bidding  = biddingService.getBiddingById(id);
					biddingService.delete(bidding);	
					JOptionPane.showMessageDialog(this, "删除成功！");
					this.refresh();
				}
			}			
		}
		else if(e.getSource()==refreshBtn){
			this.refresh();
		}
		
	}
	
}	

/**
 * 竞价功能数据模型
 * @author DCJ
 *
 */
class BiddingListModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;	
		private String[] title = {"序号","年份","产品","市场","数量","交货期","账期","竞价","状态"};
		private Object[][] data= null;
		public BiddingListModel(List<Bidding> list){
			SystemOrderService systemOrderService = Erps.getIocInstance().get(SystemOrderServiceImpl.class);
			data =  new Object[list.size()][title.length];
			Iterator<Bidding> iter = list.iterator();
			for (int i = 0; i < list.size(); i++) {
				Bidding bidding = iter.next();
				data[i][0] = bidding.getId();
				data[i][1] = bidding.getYear();
				SystemOrder order = systemOrderService.getSystemOrderById(bidding.getOrder_Id());
				data[i][2] = order.getProduct();
				data[i][3] = order.getMarket();
				data[i][4] = order.getQuantity();
				data[i][5] = order.getDeliver();
				data[i][6] = order.getQuarter();
				data[i][7] = bidding.getMyprice();
				data[i][8] = bidding.getStatus();				
			}	
	}

	@Override
	public int getRowCount() {
		return this.data.length; //行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; //列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return  data[rowIndex][columnIndex];  //取表格单元格值
	}
	public String getColumnName(int columnIndex) {
        return title[columnIndex];  //显示标题
    }

}

